Showing posts with label Dot Net. Show all posts
Showing posts with label Dot Net. Show all posts

Thursday, September 4, 2014

gpg: public key decryption failed: Bad passphrase

When I  try to decrypt a file using GPG, for which I use "Starksoft.Cryptography.OpenPGP". I got the following error
Starksoft.Cryptography.OpenPGP.GnuPGException: An error occurred while trying to execute command --list-secret-keys.
But when I execute the command through command prompt ">gpg --list-secret-keys", it does list the keys. I could not get "Starksoft.Cryptography.OpenPGP" to work correctly.
Next I tried to get a solution by running the process directly using cmd.exe, which worked perfectly.

>echo Mypasspharse|gpg.exe --passphrase-fd 0 -o "C:\successtest.txt" --decrypt "C:\testfile.txt.gpg"
Note:
If Mypassphare contain a character ">" which will get interpreted as std out redirect in windows command prompt. So, passphase will not pass to the next command properly. So make sure you are not using  ">" in Passphase, if you are planning to use command prompt for gpg decryption

Wednesday, December 11, 2013

PHP and C#.Net MD5 code mismatch

Are you experiencing the MD5 code mismatch, even though you are using the same string and key in Php and dot net? Of course it does... Since we need to convert the string + key into ASCII bites. You can use the below code in C#.Net to match the MD5 code in php.


               var strKey = "********";
               var strValue = "abcd";

                byte[] asciiBytes = ASCIIEncoding.ASCII.GetBytes(strValue  + strKey);
                byte[] hashedBytes = MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes);
                string hashedString = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();

Thursday, November 7, 2013

Parameter Count Mismatch

O boy!.... Today, I bang my head to solve this issue....

I was trying to Send an attachment email through Dot Net Amazon SDK. But, got stuck with this issue.. Since the following code is standard, that you can get from internet, to convert the MailMessage To MemoryStream. Finally identify the issue and solved.

public static MemoryStream ConvertMailMessageToMemoryStream(MailMessage message)
        {
            Assembly assembly = typeof(SmtpClient).Assembly;
            Type mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");
            MemoryStream fileStream = new MemoryStream();
            ConstructorInfo mailWriterContructor = mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(Stream) }, null);
            object mailWriter = mailWriterContructor.Invoke(new object[] { fileStream });
            MethodInfo sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic);
            sendMethod.Invoke(message, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { mailWriter, true}, null); // Throws Error , Parameter Count Mismatch
            MethodInfo closeMethod = mailWriter.GetType().GetMethod("Close", BindingFlags.Instance | BindingFlags.NonPublic);
            closeMethod.Invoke(mailWriter, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { }, null);
            return fileStream;
        }


You have to change the parameter array object. Since "Send" Private method has three parameters System.Net.Mail.MailMessage.Send(BaseWriter writer, bool sendEnvelope, bool allowUnicode).

Correct Code


public static MemoryStream ConvertMailMessageToMemoryStream(MailMessage message)
        {
            Assembly assembly = typeof(SmtpClient).Assembly;
            Type mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");
            MemoryStream fileStream = new MemoryStream();
            ConstructorInfo mailWriterContructor = mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(Stream) }, null);
            object mailWriter = mailWriterContructor.Invoke(new object[] { fileStream });
            MethodInfo sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic);
            sendMethod.Invoke(message, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { mailWriter, true, true}, null); // Success
            MethodInfo closeMethod = mailWriter.GetType().GetMethod("Close", BindingFlags.Instance | BindingFlags.NonPublic);
            closeMethod.Invoke(mailWriter, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { }, null);
            return fileStream;
        }

Thursday, July 18, 2013

System.Web.HttpException: The length of the query string for this request exceeds the configured maxQueryStringLength value

By default, the query string lengths was set to 2048 characters. To change the query strings length, modify the maxQueryStringLength attribute in httpRuntime.
It will be in the web.config file. <system.web> <httpRuntime /> </system.web>
<httpRuntime maxRequestPathLength="260" maxQueryStringLength="2048" />


Tuesday, April 2, 2013

PHP - IFrame - IE issues

I was banging my head for a week to fix the issue with sessions not maintaining in PHP iFrame in IE browser.

I thought this would be helpful for others.

In IIS7, click the site, goto Feature view, click the "HTTP Response Header"


  1. In the Custom HTTP Headers box, click Add.
  2. In the Custom Header Name text box, type p3p.
  3. In the Custom Header Value text box, add your compact codes. For example, to have the following mini header for your site

     P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM"


Tuesday, March 12, 2013

MakeCert Error: Too many parameters

Sometime, while copy pasting the command in command prompt the minus sign "-" will be understand differently, so try changing the '-' characters to the correct one. Its will work.


Error: CryptCertStrToNameW failed => 0x80092023 (-2146885597)

You get this error, when the CN contains characters not conform to X500. In your example you use a name such as "CN=norcron-develop". Here the "-" causes the problem. The same also goes for ".", "," and ";" 


Error: WriteFile failed => 0x5 (5)

Open the command prompt as admin

MakeCert.exe not found

You can find this file in Windows SDK

Path : C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin


Use the makecert and pvk2pfx tools that ships with Visual Studio (e.g. C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\Bin\makecert.exe) to create certificate files and private key files.

Makecert.exe –r –pe –n “cn=www.idp.com” –sv idp.pvk idp.cer

You then need to convert the PVK file to a PFX file so it can be loaded with the .NET framework classes.

Pvk2pfx.exe –pvk idp.pvk –spc idp.cer –pfx idp.pfx –po <password>

Refer to the Microsoft help for additional options.

Generate .pem from pfx : http://help.globalscape.com/help/eft6-2/mergedprojects/eft/exporting_a_certificate_from_pfx_to_pem.htm

Tuesday, February 26, 2013

Deployment Using NAnt

Useful link : http://www.geoffhudik.com/tech/2012/5/5/build-automation-part-1-overview-and-pre-build-tasks.html